<template>
  <div>
    <div :class="HomeContent.Default.BDT.value == '0' ? 'timerToRoll' : 'timer'">
      00:{{ formattedMinutes }}:{{ formattedSeconds }}
    </div>
    <messageBox
      ref="messageBoxRef"
      :Questions="'上传成功，点击确认退出软件'"
      :messageType="0"
    ></messageBox>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import HomeContent from "../HomeContent";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";
import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import MessageCode from "../MessageCode";
import messageBox from "../messageBox.vue";

const messageBoxRef = ref<any>();

// 倒计时
// console.log("HomeContent.Default.Duration.value", HomeContent.Default.Duration.value);

const remainingTime = ref(HomeContent.Default.Duration.value * 1000);
// const remainingTime = ref(0.1 * 60 * 1000);

// let timer: any = null;
function startCountdown() {
  HomeContent.Default.timerControl.value = setInterval(() => {
    remainingTime.value -= 1000;
    if (remainingTime.value <= 0) {
      // 模拟一次点击提交
      sub();
      clearInterval(HomeContent.Default.timerControl.value);
      HomeContent.Default.timerControl.value = null;
    }
  }, 1000);
}
const formattedMinutes = computed(() => {
  const minutes = Math.floor(remainingTime.value / 60000);
  return minutes.toString().padStart(2, "0");
});

const formattedSeconds = computed(() => {
  const seconds = Math.floor((remainingTime.value % 60000) / 1000);
  return seconds.toString().padStart(2, "0");
});

async function sub() {
  //获取评价数据
  let newArr: any = await WebSocketComponent.Default.PostMessageAsync(
    new ClientMessage.DiagnosisAdapterRequest(
      MessageCode.Default.MsgCodesList.APPRAISE_SUBMIT,
      `${HomeContent.Default.Duration.value}`
    )
  );
  console.log(JSON.parse(newArr.Content).Result);
  HomeContent.Default.Synthetical.value = JSON.parse(
    newArr.Content
  ).Result.Record.Synthetical;
  console.log("获取评价数据qqqqqqqqqqqqqqqqqqq: ", HomeContent.Default.Synthetical.value);

  HomeContent.Default.FaultRepair.value = JSON.parse(
    newArr.Content
  ).Result.Record.FaultRepair;
  HomeContent.Default.AED.value = JSON.parse(newArr.Content).Result;

  HomeContent.Default.WorksheetData.value =
    HomeContent.Default.AED.value.Record.RatingForm.RatingForm.RatingFormGroupModels;
  HomeContent.Default.PlayerReportData.value =
    HomeContent.Default.AED.value.Orders.Orders;

  if (HomeContent.Default.CacheFullData.value) {
    console.log("canshu : ", HomeContent.Default.CacheFullData.value);

    if (HomeContent.Default.CacheFullData.value.FaultPointCollection) {
      console.log(
        "提交参数： ",
        HomeContent.Default.CacheFullData.value.FaultPointCollection
      );

      let license = JSON.parse(newArr.Content).Result;

      license.Orders.OrderBase.Time = HomeContent.Default.Duration.value;

      license.Id = HomeContent.Default.CacheFullData.value.ResultId;
      license.Orders = JSON.stringify(license.Orders);
      license.Record = JSON.stringify(license.Record);
      license.Tool = 0;
      license.Standard = 0;
      license.Process = 0;
      license.Security = 0;
      license.Efficiency = 0;
      license.Time = HomeContent.Default.AED.value.UsedTime;
      console.log(
        "????????????????????????????",
        HomeContent.Default.CacheFullData.value
      );

      // if (HomeContent.Default.CacheFullData.value.FaultPointCollection.Mode === "4") {
      if (HomeContent.Default.CacheFullData.value.Mode == "4") {
        let response = await fetch(
          // "http://virtual.eduhenghong.cn:4497/api/TrainingResult/Submit",
          "http://virtual.eduhenghong.cn:4497/api/SkillsContest/SubmitResult",
          {
            method: "POST",
            body: JSON.stringify(license),
            headers: {
              "Content-Type": "application/json",
              Authorization: `Bearer ${HomeContent.Default.CacheFullData.value.Token}`,
            },
          }
        );
        console.log("Mode为4", response);
      } else {
        let response = await fetch(
          "http://virtual.eduhenghong.cn:4497/api/TrainingResult/Submit",
          // "http://virtual.eduhenghong.cn:4497/api/SkillsContest/SubmitResult",
          {
            method: "POST",
            body: JSON.stringify(license),
            headers: {
              "Content-Type": "application/json",
              Authorization: `Bearer ${HomeContent.Default.CacheFullData.value.Token}`,
            },
          }
        );
        console.log("Mode不是4", response);
      }
    }
  }
  if (HomeContent.Default.BDT.value == "0") {
    HomeContent.Default.EvaluateShow.value = true;
  } else {
    messageBoxRef.value.showModel();
  }
  HomeContent.Default.SubBut.value = false;
}

defineExpose({
  startCountdown,
});
</script>

<style scoped>
.timer {
  width: 93px;
  height: 40px;
  line-height: 40px;
  display: inline-block;
  margin-left: 1078px;
  font-size: 25px;
  position: absolute;
  top: 7px;
  left: 514px;
}

.timerToRoll {
  width: 140px;
  height: 40px;
  line-height: 40px;
  display: inline-block;
  margin-left: 1078px;
  font-size: 25px;
  position: absolute;
  top: 7px;
  /* left: 480px; */
  left: 528px;
}
</style>
